package com.ionehe.group.common.wechat.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.ionehe.group.common.wechat.WechatService;
import com.ionehe.group.common.wechat.component.WechatAPI;
import com.ionehe.group.common.wechat.component.WechatOpenidCache;
import com.ionehe.group.common.wechat.component.WxBizDataCrypt;
import com.ionehe.group.common.wechat.config.WechatConfig;
import com.ionehe.group.common.wechat.enums.WechatEnv;
import com.ionehe.group.common.wechat.exception.WechatException;
import com.ionehe.group.common.wechat.model.Code2Session;
import com.ionehe.group.common.wechat.model.PhoneInfoResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author 2022/1/7 <a href="mailto:a@ionehe.com">xiu</a>
 */
@Component
@Slf4j
public class WechatServiceImpl implements WechatService {
    @Resource
    private WechatOpenidCache openidCache;
    @Resource
    private WechatAPI wechatApi;
    @Resource
    private WechatConfig config;

    @Override
    public String getOpenid(String code, WechatEnv env) {
        String openId = openidCache.findOpenId(code, config, env);
        log.info("WechatService[]getOpenid[] code:{},openId:{},env:{}", code, openId, env);
        return openId;
    }

    @Override
    public String getSessionKey(String code) {
        if (StrUtil.isBlank(code)) {
            log.warn("WechatService[]getSessionKey[]get session key failed, code:{}", code);
            throw new WechatException("code null error");
        }

        Code2Session code2Session = wechatApi.getOpenIdMiniPro(code, config);

        return code2Session.getSessionKey();
    }

    @Override
    public PhoneInfoResponse.PhoneInfo getUserPhoneInfo(String code) {
        if (StrUtil.isBlank(code)) {
            log.warn("WechatService[]getUserPhoneInfo[]get user phone info failed, code:{}", code);
            throw new WechatException("code null error");
        }

        return wechatApi.getUserPhoneNumber(code, config);
    }

    @Override
    public Byte[] getQrCodeUnLimit(String page, Integer width, Map<String, String> param) {
        if (StrUtil.isBlank(page)) {
            log.warn("WechatService[]getQrCodeUnLimit[]get un limit qrcode failed, page:{}", page);
            throw new WechatException("page null error");
        }

        return wechatApi.getQrCodeUnLimit(page, width, param, config);
    }

    @Override
    public JSONObject decryptData(String code, String encryptedData, String iv) {
        if (StrUtil.isBlank(code) || StrUtil.isBlank(encryptedData) || StrUtil.isBlank(iv)) {
            log.warn("WechatService[]decryptData[] decrypt data failed, code:{}, encryptedData:{}, iv:{}", code, encryptedData, iv);
            throw new WechatException("authorization code or encrypted data or iv null error");
        }

        WxBizDataCrypt wxBizDataCrypt = new WxBizDataCrypt(config.getAppID(), getSessionKey(code));
        return wxBizDataCrypt.decryptData(encryptedData, iv);
    }
}
